package conson.tech.manage.app.controller.mobile;

import cn.hutool.json.JSONUtil;
import conson.tech.manage.api.app.dto.ChannelAdvertDTO;
import conson.tech.manage.api.app.dto.ChannelAdvertDataDTO;
import conson.tech.manage.api.app.dto.ChannelIconConfigDTO;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.response.Response;
import conson.tech.manage.app.service.ChannelAdService;
import conson.tech.manage.app.service.ChannelIconService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: ChannelAdIconMobileController
 * Description: 渠道广告与图标相关控制器
 * Author: HXL
 * Date: 2020/4/22 3:44 下午
 * Version: 1.0
 */
@Slf4j
@Api(value = "渠道广告与图标相关控制器",tags = "渠道广告与图标相关控制器")
@RestController
public class ChannelAdIconMobileController {
    @Autowired
    private ChannelAdService channelAdService;
    @Autowired
    private ChannelIconService channelIconService;
    @PostMapping(SecurityConstants.MOBILEAPI_MATCHER+"/channel/queryAdIcon")
    public Response<ChannelAdvertDataDTO> queryChannelAdIconData(HttpServletRequest request, HttpServletResponse response){
        String channelCode = request.getHeader("Application-Code");
        if (StringUtils.isEmpty(channelCode)){
           return Response.fail("请传递渠道号");
        }
        log.info("====== 进入【渠道广告和icon查询接口】ChannelAdIconMobileController --->queryChannelAdIconData param:[{}]",
                JSONUtil.toJsonStr(channelCode));
        ChannelAdvertDataDTO channelAdvertDataDTO = new ChannelAdvertDataDTO();
        if( StringUtils.isEmpty(channelCode)){
            channelAdvertDataDTO.setBannerList(new ArrayList<>());
            channelAdvertDataDTO.setIconList(new ArrayList<>());
            return Response.success(channelAdvertDataDTO);
        }
        List<ChannelAdvertDTO>  adList = channelAdService.queryAdvertByChannelCode(channelCode);
        List<ChannelIconConfigDTO>  iconList = channelIconService.queryIconByChannelCode(channelCode);
        channelAdvertDataDTO.setBannerList(adList);
        channelAdvertDataDTO.setIconList(iconList);
        return Response.success(channelAdvertDataDTO);
    }
}
